﻿package  
{	
	import flash.net.XMLSocket
	import flash.events.Event
	import flash.events.MouseEvent
	import flash.events.EventDispatcher
	import flash.events.DataEvent
	import flash.events.IOErrorEvent
	import flash.xml.XMLDocument
	import flash.xml.XMLNode
	import flash.xml.XMLNodeType
	import BlinkEvent
	
	public class BlinkClient extends EventDispatcher
	{
		var Connection : XMLSocket;

		public function BlinkClient(host:String,port:int)
		{
			Connection = new XMLSocket(host,port)
			this.Connection.addEventListener( flash.events.Event.CONNECT, this.onConnect )
			this.Connection.addEventListener( DataEvent.DATA, this.dataHandler )

			this.addEventListener( "xml.room"  , this.doRoomNode) 
			this.addEventListener( "xml.user"  , this.doUserNode) 
		}
		
		public function t(txt)
		{
			trace(txt)
		}
		
		public function chat(txt)
		{
			var xml = new XMLDocument("<blink></blink>");
			var r = new XMLNode(XMLNodeType.ELEMENT_NODE ,"room");
			var c = new XMLNode(XMLNodeType.ELEMENT_NODE ,"chat");
			var t = new XMLNode(XMLNodeType.TEXT_NODE,txt);
			
			c.appendChild(t)
			r.appendChild(c)
			xml.firstChild.appendChild( r )
			
			this.sendXml(xml);
		}
		
		public function login(name)
		{
			var xml = new XMLDocument("<blink></blink>");
			var login = new XMLNode(XMLNodeType.ELEMENT_NODE ,"login");
			var u = new XMLNode(XMLNodeType.ELEMENT_NODE ,"user");
			
			login.attributes["name"] = name;
			u.appendChild( login )
			xml.firstChild.appendChild( u )
			
			this.sendXml(xml);
		}

		public function getRoomInfo(detail=true)
		{
			var xml = new XMLDocument("<blink></blink>");
			var req = new XMLNode(XMLNodeType.ELEMENT_NODE ,"info");
			var u = new XMLNode(XMLNodeType.ELEMENT_NODE ,"room");
			
			req.attributes["detail"] = detail?"yes":"no";
			u.appendChild( req )
			xml.firstChild.appendChild( u )
			
			this.sendXml(xml);
		}
		
		public function sendXml(xmlDoc)
		{
			this.Connection.send(xmlDoc)
		}
		
		public function onConnect(e:Event)
		{
			dispatchEvent(new BlinkEvent(  BlinkEvent.CONNECT ,null ))
		}

        private function dataHandler(event:DataEvent):void 
		{
			t("recv "+event.data)
			var xmlDoc:XMLDocument = new XMLDocument(event.data)
            for(var i in xmlDoc.firstChild.childNodes )
			{
				var node  = xmlDoc.firstChild.childNodes[i]
				dispatchEvent(new BlinkEvent(  "xml."+node.nodeName , null, "", node ))
			}
        }
		
		private function doRoomNode(e:BlinkEvent):void
		{
			for each(var node in e.xmlNode.childNodes )
			{
				switch(node.nodeName)
				{
					case 'chat' : dispatchEvent(new BlinkEvent(BlinkEvent.CHAT_INCOME ,node.attributes , node.firstChild.nodeValue ,node ))
								  break;
					case 'room' : dispatchEvent(new BlinkEvent(BlinkEvent.ROOM_INFO ,node.attributes , node.firstChild.nodeValue ,node ))
								  break;
					case 'leave': dispatchEvent(new BlinkEvent(BlinkEvent.USER_LEAVES ,node.attributes , node.firstChild.nodeValue ,node ))
								  break;
					case 'join' : dispatchEvent(new BlinkEvent(BlinkEvent.USER_JOINS ,node.attributes , node.firstChild.nodeValue ,node ))
								  break;
					case 'error': dispatchEvent(new BlinkEvent(BlinkEvent.ERROR ,node.attributes , node.firstChild.nodeValue ,node ))
							      break;
				}
			}
		}
		
		private function doUserNode(e:BlinkEvent):void
		{
			for each(var node in e.xmlNode.childNodes )
			{
				switch(node.nodeName)
				{
					case 'login': dispatchEvent(new BlinkEvent(BlinkEvent.LOGIN_RESULT ,node.attributes , null ,node ))
							      break;
				}
			}
		}
	}
}
